var ctx;
var regionId;
var buildingId;

function setCtx(ctx) {
	this.ctx = ctx;
	initListener();  //添加区域监听事件
}

//编辑时进到画面显示当前楼栋
$(function(){
	buildingByRegion();
})

//验证单元名
function checkUnitName(){
	var unitName = $("#cellunitName").val();
	//解决ie9以下placeholder与value重复的问题
	if(unitName == $("#cellunitName").attr("placeholder")){
		unitName = "";
	}
	if(unitName == null || unitName == ""){
		layer.tips('单元名不能为空!', '#cellunitName', {
		    tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证区域名
function checkRegionName(){
	var regionName = $("#regionid").val();
	if(regionName == null || regionName == ""){
			layer.tips('请选择区域名!', '#regionid', {
				tips: 3,
				time:1000
			});
		return false;
	}else{
		return true;
	}
}
/**
 * 验证楼栋名
 */
function checkBuildingName(){
	var building = $("#buildingid").val();
	if(building == null || building == ""){
		layer.tips('请选择楼栋名!', '#buildingid', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证排序不能为空
function checkSort(){
	var reg = /^[0-9]*[1-9][0-9]*$/;
	var sort = $("#sort").val();
	if(sort == null || sort == ""){
		layer.tips('排序不能为空!', '#sort', {
			tips: 3,
			time:1000
		});
		return false;
	}else if(!reg.test(sort)){
		layer.tips('请输入正整数!', '#sort', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

//小区、区域级联
function initListener(){
	/**
	 * 根据区域id 查询相应区域下所以的楼栋信息
	 */
	$("#regionid").change(function(){
		buildingByRegion();
	});
	
	$('#buildingid').change(function() {
		buildingId = $('#buildingid').val();
    });
}

function buildingByRegion(){
	regionId = $('#regionid').val();
	buildingId = $('#buildingId').val();
	if (regionId != ""){
		$.ajax({
	        type : "GET",
	        url : ctx + "/cellUnit/getBuildingByRegionId",
	        cache : false,
	        dataType : 'json',
	        data : {
	        	regionid : regionId,
	        },
	        success : function(data) {
	        	if (data.success) {
	        		var result = "<option value='" + "'>--楼栋--</option>";
	                $.each(data.obj, function(index, item) {
	                	if(item.id == buildingId){
	                		result += "<option id='" + item.id + "'" +" selected='selected'" + " value='" + item.id + "'>" + item.buildingName + "</option>";
	                	}else{
	                		result += "<option id='" + item.id + "'" + " value='" + item.id + "'>" + item.buildingName + "</option>";
	                	}
	                });
	                $("#buildingid").html("");
	                $("#buildingid").append(result);
	                $('#buildingid').change();
	            }
	        }
		});
	} else {
		var result = "<option value='" + "'>--楼栋--</option>";
		$("#buildingid").html("");
        $("#buildingid").append(result);
        $('#buildingid').change();
	}
}

//新增或编辑小区区域(1:新增 2:编辑)
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
function saveOrUpdateCellUnit(type){
	var id;
	if($("#cellunitId").val() != null && $("#cellunitId").val() != ''){
		id = $("#cellunitId").val();
	}
	var name = $("#cellunitName").val();
	var memo = $("#memo").val();
	var sort = $("#sort").val();
	var regionid = $("#regionid").val();
	var buildingid = $("#buildingid").val();
	var cellName = $("#cellid").val();
	var regionName = $("#regionid option:selected").text();
	var buildingName = $("#"+buildingid).text();

	if(checkUnitName() && checkBuildingName() && checkRegionName() && checkSort()){
		var index1 = layer.load(1);
		$.post(ctx+"/cellUnit/saveOrUpdateCellunit",
				{
			id:id,
			name:name,
			regionid:regionid,
			buildingid:buildingid,
			cellName:cellName,
			regionName:regionName,
			buildingName:buildingName,
			sort:sort,
			memo:memo
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("操作成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							parent.loadContent("/cellUnit/cellUnitList");
							parent.layer.close(index);
						});
					} else {
						layer.close(index1);
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
	}
}